package com.hunktimes.wholesale.net

import com.hunktimes.wholesale.net.model.*
import retrofit2.http.Query

/**
 * Author: Delete
 * Date: 2020/7/22 17:39
 * Description:
 *
 */
object WholeSaleRepository {

    //val api: IServer = ServiceCreater.create(IServer::class.java)
    val api = ServiceCreater.create<IServer>()

//    init {
//        api = IServer.get()
//    }

    /**
     * 判断业务吗
     */
    fun <T> preFilterResp(resp: ApiResponse<T>): ApiResponse<T> {
        return if (resp.isSuccess())
            resp
        else if (resp.code == 401 || resp.code == 403)
            throw TokenInvalidException(resp.code, resp.message ?: "未知异常")
        else
            resp
    }

    /**
     * 先判断 业务吗
     * 再 拆包
     */
    fun <T> preFilterUnpack(resp: ApiResponse<T>): T? {
        return preFilterResp(resp).data
    }

    suspend fun login(loginOrReg: LoginOrReg): ApiResponse<String>? {
        val resp = api.login(loginOrReg)
        return preFilterResp(resp)
    }

    suspend fun getSmsCode(getCaptcha: GetCaptcha): ApiResponse<Any?> {
        val resp = api.getSmsCode(getCaptcha)
        return resp
    }

    suspend fun forgetPwd(forgetPwd: ForgetPwd): ApiResponse<Any?> {
        val resp = api.forgetPwd(forgetPwd)
        return resp
    }

    suspend fun getBanners(): ApiResponse<List<String>> {
        val resp = api.getBanners()
        return resp
    }

    suspend fun getHomeData(): ApiResponse<List<String>> {
        val resp = api.getBanners()
        return resp
    }

    suspend fun getCategoryTags(): ApiResponse<List<String>> {
        val resp = api.getCategoryTags()
        return resp
    }

    suspend fun queryCategoryByTag(tag: String): ApiResponse<List<String>> {
        val resp = api.queryCategoryByTag(tag)
        return resp
    }

    suspend fun getCartGoods(): ApiResponse<List<String>> {
        val resp = api.getCartGoods()
        return resp
    }

    suspend fun getCartRecommends(): ApiResponse<List<String>> {
        val resp = api.getCartRecommends()
        return resp
    }

    suspend fun getUserInfo(): ApiResponse<List<String>> {
        val resp = api.getCartRecommends()
        return resp
    }

    suspend fun queryOrdersByType(type: String): ApiResponse<List<String>> {
        val resp = api.queryOrdersByType(type)
        return resp
    }

    suspend fun getCollections(): ApiResponse<List<String>> {
        val resp = api.getCollections()
        return resp
    }


    suspend fun queryProdDetail(productId: String): ApiResponse<List<String>> {
        val resp = api.queryProdDetail(productId)
        return resp
    }


    suspend fun getDefRecAddr(): ApiResponse<String> {
        val resp = api.getDefRecAddr()
        return resp
    }

    suspend fun getRecAddrList(): ApiResponse<List<String>> {
        val resp = api.getRecAddrList()
        return resp
    }


    suspend fun updateAddress(recAddress: RecAddress): ApiResponse<String> {
        val resp = api.updateAddress(recAddress)
        return resp
    }


    suspend fun updateMemberInfo(memberInfo: MemberInfo): ApiResponse<String> {
        val resp = api.updateMemberInfo(memberInfo)
        return resp
    }

}